package com.bootdo.utils;

import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import com.lowagie.text.pdf.PdfReader;
import org.apache.pdfbox.text.PDFTextStripper;

public class PDFToIMGUtil {

    private static Logger logger = Logger.getLogger(PDFToIMGUtil.class);

    public static void main(String[] args) {
        pdf2Word("D:\\document\\temp\\gzsc.pdf");
    }

    /***
     * PDF文件转PNG图片，全部页数
     *
     * @param PdfFilePath pdf完整路径
     * @param dpi dpi越大转换后越清晰，相对转换速度越慢
     * @return
     */
    public static String pdf2Png(String PdfFilePath, String imgFolderPath, int dpi) {
        File file = new File(PdfFilePath);
        String descPath = null;
        PDDocument pdDocument;
        try {
            int dot = file.getName().lastIndexOf('.');
            String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
            imgFolderPath = file.getParent();;
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            /* dpi越大转换后越清晰，相对转换速度越慢 */
            PdfReader reader = new PdfReader(PdfFilePath);
            int pages = reader.getNumberOfPages();
            StringBuffer imgFilePath = null;
            for (int i = 0; i < 1; i++) {
                String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
                imgFilePath = new StringBuffer();
                imgFilePath.append(imgFilePathPrefix);
                imgFilePath.append(".png");
                File dstFile = new File(imgFilePath.toString());
                descPath =  dstFile.getAbsolutePath();
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                ImageIO.write(image, "png", dstFile);
            }
            logger.info("PDF文档转PNG图片成功！");

        } catch (IOException e) {
            logger.error("PDF文档转PNG图片的异常："+e.getMessage());
        }
        return descPath;
    }


    public static String pdf2PngPath(String PdfFilePath, String dstPath, int dpi) {
        File file = new File(PdfFilePath);
        String descPath = null;
        PDDocument pdDocument;
        try {
            int dot = file.getName().lastIndexOf('.');
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            /* dpi越大转换后越清晰，相对转换速度越慢 */
            PdfReader reader = new PdfReader(PdfFilePath);
            int pages = reader.getNumberOfPages();
            for (int i = 0; i < 1; i++) {
                File dstFile = new File(dstPath);
                descPath =  dstFile.getAbsolutePath();
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                ImageIO.write(image, "png", dstFile);
            }
            logger.info("PDF文档转PNG图片成功！");

        } catch (IOException e) {
            logger.error("PDF文档转PNG图片的异常："+e.getMessage());
        }
        return descPath;
    }

    public static void pdf2Word(String source) {
        try {
            String pdfFile = source;
            PDDocument doc = PDDocument.load(new File(pdfFile));
            int pagenumber = doc.getNumberOfPages();
            pdfFile = pdfFile.substring(0, pdfFile.lastIndexOf("."));
            String fileName = pdfFile + ".doc";
            File file = new File(fileName);
            if (!file.exists())
            {
                file.createNewFile();
            }
            FileOutputStream fos = new FileOutputStream(fileName);
            Writer writer = new OutputStreamWriter(fos, "UTF-8");
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);// 排序
            stripper.setStartPage(1);// 设置转换的开始页
            stripper.setEndPage(pagenumber);// 设置转换的结束页
            stripper.writeText(doc, writer);
            writer.close();
            doc.close();
            System.out.println("pdf转换word成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
